// 2013-10-25下午03:11:18  net.betou.trade.admin.action.FundsAct.java

package net.betou.trade.admin.action;

import javax.servlet.http.HttpServletRequest;

import net.betou.common.util.TradeUtils;
import net.betou.trade.action.abs.ActionSupport;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Funds;
import net.betou.trade.main.manager.FundsMng;
import net.betou.trade.main.manager.agencyorproxy.AgencyMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.WebErrors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 可分配短信用户：
 * 1. 系统管理员 - 直接使用短信
 * 2. 机构 - 由系统管理员来指定
 * 3. 代理商 - 由机构来指定
 * @author 叶装装
 */
@Controller
public class FundsAct extends ActionSupport< Funds >{

	/**
	 *  进入短信分配列表
	 *  代理商不具备该功能
	 * @param id		机构或者代理商的ID
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping( value = "/funds/v_list.jspx" )
	public String list( Integer id, HttpServletRequest request, ModelMap model ) {
		
		AgentAndAdmins currentAdmin = TradeUtils.getAdmin( request );
		
		WebErrors errors = validateToFunds( currentAdmin, id ); //validateToList( id );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		AgentAndAdmins admin = mng.findById( id ); 
		Funds funds = admin.getFundsAccount( );
		
		Integer smsNum = 0;
		
		if( funds != null ){
			smsNum = funds.getSmsNum( );
		}
		
		model.addAttribute( "bean", admin );
		model.addAttribute( "smsNum", smsNum );
		model.addAttribute( "admin", currentAdmin );
		
		return getListPage( );
	}
	
	// 进行账户编辑
	@RequestMapping( value = "/funds/o_update.jspx" )
	public String update( Funds funds, Integer beanId,  HttpServletRequest request , ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); // 获取当前管理员
		
		// 如果当前管理员是机构
		if( admin.getAgency( ) ){
			Funds funds2 = new Funds( );
			// 则需要更新机构的数量
			funds2.setSmsNum( admin.getSmsNum( ) - funds.getSmsNum( ) );
			fundsMng.update( funds2, admin );
		}
		
		AgentAndAdmins bean = mng.findById( beanId );
		
		funds.setSmsNum( bean.getSmsNum( ) + funds.getSmsNum( ) );
		
		fundsMng.update( funds, bean );
		
		return "redirect:v_list.jspx?id=" + bean.getId( );
	}
	
	private WebErrors validateToFunds( AgentAndAdmins currentAdmin, Integer id ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		AgentAndAdmins admin = mng.findById( id ); // 查找机构或者代理商
		
		if( admin == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		// 如果是机构， 分配的人必须是系统管理员
		if( admin.getAgency( ) ){
			if(! currentAdmin.getSystem( ) ){
				errors.addError( Messages.FUNDS_ADMIN_NOT_OPERATING );
				return errors;
			}
		}
		
		// 如果是代理商，分配的人必须是机构
		if( admin.getProxy( ) ){
			if(! currentAdmin.getAgency( ) && !currentAdmin.getSystem( ) ){
				errors.addError( Messages.FUNDS_ADMIN_NOT_OPERATING );
				return errors;
			}
		}
		
		return errors;
	}
	
	@Autowired
	private AgencyMng mng;
	
	@Autowired
	private FundsMng fundsMng;
	
	public static final String DIRECTORY = "funds";

	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}

}
